#include <iostream.h>
#include "Record.h"

int main () {
	cout << "What do you want to do?\n";
	cout << "1. Load a text file into a binary database file.\n";
	cout << "2. Apply a selection predicate to a binary database file.\n";
	cout << "3. Find all records falling in a range using a B+-Tree.\n";
	cout << "4. Find all records falling in a range using a spatial index.>\n";
	int choice;
	cin >> choice;

	if (choice == 1) {

		cout << "What is the name of the text file to load?\n";
		char ans1[100];
		cin >> ans1;

		cout << "What is the name of the file where the schema is located?\n";
		char ans2[100];
		cin >> ans2;

		cout << "What is the name of the relation schema?\n";
		char ans3[100];
		cin >> ans3;

		cout << "Where should the binary file be stored?\n";
		char ans4[100];
		cin >> ans4;

		// load in the relation's schema
		Schema mySchema (ans2, ans3);

		// open up the file
		File dbFile;
		dbFile.Open (0, ans4);

		// this will store the data
		Record temp;

		// this is the text file to process
		FILE *inFile = fopen (ans1, "r");

		// load it up
		int i;
		while (temp.SuckNextRecord (mySchema, inFile)) {
			if (i % 1000 == 0)
				cerr << ".";
			dbFile.Append (temp);
			i++;
		}
		cerr << "Done!\n";

		// and close it
		dbFile.Close ();

	} else {

		cout << "What is the name of the file where the schema is located?\n";
		char ans1[100];
		cin >> ans1;

		cout << "What is the name of the relation schema?\n";
		char ans2[100];
		cin >> ans2;

		cout << "What is the name of the file where the selection predicate is stored?\n";
		char ans3[100];
		cin >> ans3;

		cout << "What is the name of the binary data file?\n";
		char ans4[100];
		cin >> ans4;

		// load in the relation's schema
		Schema mySchema (ans1, ans2);

		// load in the predicate
		Predicate myPred (mySchema, ans3);

		// open up the file
		File dbFile;
		dbFile.Open (1, ans4);

		// and scan the file
		Record temp;
		int i = 0;
		while (dbFile.NextRec (temp)) {
			i++;
			if (myPred.Apply (temp)) {
				temp.Print (mySchema);
			}
		}
	}

}
